<template>

  <div class="home" v-if="homelist">
      <div class="totop" @click="totop" v-if="flag">
        <img src="../assets/img/top.png" alt="" />
      </div>
      <div class="banner">
        <van-swipe class="my-swipe" :autoplay="3000" indicator-color="white">
          <van-swipe-item
            v-for="(item, index) in homelist[0].banner"
            :key="index"
          >
            <div class="img">
              <router-link
                tag="img"
                :to="'/item?id=' + item.id"
                :src="item.imgurl"
                alt=""
              />
            </div>
          </van-swipe-item>
        </van-swipe>
      </div>
      <div class="search">
        <van-sticky @change="testCet" v-show="!searchflag">
          <div class="search_content" @click="search">
            <span>
              <img src="../assets/img/search.png" alt="" />
            </span>
            <p>请输入搜索关键字</p>
          </div>
        </van-sticky>
        <van-sticky @change="testCet" v-show="searchflag">
          <div class="search_contentupdata" @click="search">
            <div class="box">
              <span>
                <img src="../assets/img/search.png" alt="" />
              </span>
              <p>请输入搜索关键字</p>
            </div>
          </div>
        </van-sticky>
      </div>
      <span class="head-bg"></span>
      <div
        class="content"
        v-for="(item, index) in homelist[3].show"
        :key="index"
      >
        <router-link tag="div" :to="'/item?id='+3" class="show">
          <img :src="item.headurl" alt="" />
        </router-link>
        <div class="post">
          <ul>
            <router-link
              tag="li"
              :to="'/item?id='+ 4"
              v-for="(k,index) in item.postlist"
              :key="index"
            >
              <img :src="k.postimgurl" alt="" />
            </router-link>
          </ul>
        </div>
      </div>
      <h2>更多商品</h2>
      <div class="more">
        <div class="mleft">
          <ul>
            <router-link
              tag="li"
              :to="'/item?id=' + item.id"
              v-for="(item,index) in homelist[1].morelistleft"
              :key="index"
            >
              <img :src="item.moreurl" alt="" />
              <p>{{ item.name }}</p>
              <p class="con">￥{{ item.price }}</p>
            </router-link>
          </ul>
        </div>
        <div class="mright">
          <ul>
            <router-link
              tag="li"
              :to="'/item?id=' + item.id"
              v-for="(item,index) in homelist[2].morelistright"
              :key="index"
            >
              <img :src="item.moreurl" alt="" />
              <p>{{ item.name }}</p>
              <p class="con">￥{{ item.price }}</p>
            </router-link>
          </ul>
        </div>
      </div>

    <buttomtag></buttomtag>
  </div>
</template>

<script>
import buttomtag from "../components/TestoCom.vue";
export default {
  props: ["homelist"],
  data() {
    return {
      searchflag: false,
      postlist: null,
      flag: false,
      top: null,
    };
  },
  methods: {
    //输入框状态的改变
    testCet(isFixed) {
      this.searchflag = isFixed;
    },
    search() {
      this.$router.push("./search");
    },
    //点击回到顶部效果
    totop() {
      // window.scrollTo(0, 0);
      var timer = setInterval(function(){
                    //获取滚动条的滚动高度
                    var osTop = document.documentElement.scrollTop || document.body.scrollTop;
                    //用于设置速度差，产生缓动的效果
                    var speed = Math.floor(-osTop / 6);
                    document.documentElement.scrollTop = document.body.scrollTop = osTop + speed;
                   var isTop =true;  //用于阻止滚动事件清除定时器
                   console.log(isTop);
                    if(osTop == 0){
                        clearInterval(timer);
                    }
                },30);
    },
    handleScroll() {
      this.top = Math.floor(
        document.body.scrollTop ||
          document.documentElement.scrollTop ||
          window.pageYOffset
      );
      // console.log(this.top);
      if (this.top > 700) {
        this.flag = true;
      }
    },
  },
  watch: {
    top: function () {
      if (this.top <= 700) {
        this.flag = false;
      }
    },
  },

  created() {},

  components: {
    buttomtag,
  },
  mounted() {
    window.addEventListener("scroll", this.handleScroll, true);
  },
};
</script>

<style lang="scss" >

.my-swipe .van-swipe-item {
  color: #fff;
  font-size: 20px;
  line-height: 150px;
  text-align: center;
  background-color: #39a9ed;
}
.home {
  .totop {
    width: 40px;
    height: 40px;
    background-color: #dcdada;
    border-radius: 10px;
    position: fixed;
    bottom: 150px;
    right: 20px;
    z-index: 99;
    img {
      display: block;
      margin: 10px auto;
      width: 20px;
      height: 20px;
    }
  }
  .banner {
    position: relative;
    .img {
      img {
        width: 100%;
        height: 223px;
      }
    }
  }
  .search {
    position: absolute;
    top: 5%;
    left: 4%;
    width: 92%;
    background: hsla(0, 0%, 100%, 0.98);
    border-radius: 5px;
    .search_content {
      height: 30px;
      display: flex;
      span {
        display: block;
        padding: 2px;
        padding-top: 3px;
        img {
          width: 25px;
        }
      }
      p {
        font-size: 15px;
        line-height: 30px;
        font-weight: 600;
        color: rgba(0, 0, 0, 0.15);
      }
    }
    .search_contentupdata {
      background-color: #fff;
      height: 50px;
      width: 100%;

      .box {
        width: 92%;
        height: 30px;
        border: 1px solid #dfdede;
        border-radius: 4px;
        background-color: rgba(0, 0, 0, 0.03);
        position: relative;
        top: 10px;
        left: 4%;
        display: flex;
        span {
          display: block;
          padding: 2px;
          padding-top: 3px;
          img {
            width: 25px;
          }
        }
        p {
          font-size: 15px;
          line-height: 30px;
          font-weight: 600;
          color: rgba(0, 0, 0, 0.15);
        }
      }
    }
  }
  .head-bg {
    position: absolute;
    top: 5px;
    left: 47%;
    display: block;
    width: 25px;
    height: 25px;
    background-image: url(../assets/img/title.png);
    background-repeat: no-repeat;
    background-size: cover;
  }
  .content {
    .show {
      width: 100%;
      img {
        display: block;
        width: 100%;
      }
    }
    .post {
      width: 100%;
      ul {
        margin: 0;
        padding: 0;
        list-style: none;
        width: 100%;
        display: flex;
        flex-wrap: wrap;
        li {
          width: 50%;
          img {
            width: 100%;
          }
        }
      }
    }
  }
  h2 {
    width: 100%;
    padding: 0px 25px;
    color: rgb(102, 102, 102);
    font-size: 18px;
    font-weight: 700;
    box-sizing: border-box;
  }
  .more {
    display: flex;
    justify-content: space-around;
    width: 95%;
    margin: 0 auto;
    padding-bottom: 70px;
    .mleft,
    .mright {
      width: 48%;
      ul {
        list-style: none;
        margin: 0;
        li {
          margin-top: 5px;

          background-color: rgb(255, 255, 255);
          img {
            width: 100%;
          }
          p {
            margin: 0;
            padding: 0;
            font-size: 14px;
            color: #666;
            margin-left: 15px;
          }
          .con {
            color: red;
          }
        }
      }
    }
  }
}
</style>